package org.openapitools.service;

import org.openapitools.model.Dog;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lemon
 */
@Service
public class DogService {

    public static List<Dog> staticDogs = new ArrayList<>();

    public Dog addPet(Dog dog) {
        if (dog == null){
            return null;
        }
        dog.setId(System.currentTimeMillis()+"");
        staticDogs.add(dog);
        return dog;
    }

    public List<Dog> getPet(String petId) {
        List<Dog> dogs = new ArrayList<>();
        for (Dog dog : staticDogs) {
            if (dog.getId().equals(petId)) {
                dogs.add(dog);
            }
        }
        return dogs;
    }

   public List<Dog> getPets() {
        return staticDogs;
    }

    public Dog updatePet(Dog dog) {
        for (int i = 0; i < staticDogs.size(); i++) {
            if (staticDogs.get(i).getId().equals(dog.getId())) {
                staticDogs.set(i, dog);
                return dog;
            }
        }
        return dog;
    }

    public boolean deletePet(String petId) {
        for (int i = 0; i < staticDogs.size(); i++) {
            if (staticDogs.get(i).getId().equals(petId)) {
                staticDogs.remove(i);
                return true;
            }
        }
        return false;
    }

    public List<Dog> getPetByName(String name) {
        List<Dog> dogs = new ArrayList<>();
        for (Dog dog : staticDogs){
            if (dog.getName().equals(name)){
                dogs.add(dog);
            }
        }
        return dogs;
    }
}
